package com.bookmarket.controller;

import com.bookmarket.entity.Btype;
import com.bookmarket.entity.Order_Book;
import com.bookmarket.service.HJHStatisticalService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/statistical")
public class HJHStatisticalController {

    @Resource
    private HJHStatisticalService hJHStatisticalService;

    @RequestMapping("/findAllBtype")
    public List<Btype> findAllBtype(){
        List<Btype> list=hJHStatisticalService.findAllBtype();
        return list;
    }
    /*业务分析
    1.计算出每个大分类的总金额.
         循环大分类，嵌套循环订单,算出总金额放入集合中。
    2.将每个大种类的总金额放进一个集合中
        返回集合
    * */
    @RequestMapping("/TotalAmount")
    public List<Float> TotalAmount(){
        List<Float> list= new ArrayList<>();
        List<Order_Book> list2=hJHStatisticalService.findAllOrder_Book();
        List<Btype> list1=hJHStatisticalService.findAllBtype();

        for (Btype b:list1) {
            float temp=0;
            for (Order_Book ob:list2) {
                float total=0;
                //System.out.println(b.getType()+":"+ob. getBook().getStype().getBtype().getType());
                if(b.getType().equals(ob.getBook().getStype().getBtype().getType())){
                   // System.out.println(ob.getAmount()+":"+ob.getBook().getPrice());
                    total=(ob.getAmount())*(ob.getBook().getPrice());
                    temp+=total;
                }else{
                    total=0;
                }
            }

            list.add(temp);

            //System.out.println(list);
        }
        return list;
    }

    @RequestMapping("/TotalNum")
    public List<Integer> TotalNum(){
        List<Integer> list= new ArrayList<>();
        List<Order_Book> list2=hJHStatisticalService.findAllOrder_Book();
        List<Btype> list1=hJHStatisticalService.findAllBtype();

        for (Btype b:list1) {
            int temp=0;
            for (Order_Book ob:list2) {
                int total=0;
                //System.out.println(b.getType()+":"+ob. getBook().getStype().getBtype().getType());
                if(b.getType().equals(ob.getBook().getStype().getBtype().getType())){
                    //System.out.println(ob.getAmount()+":");
                    total=ob.getAmount();
                    temp+=total;
                }else{
                    total=0;
                }
            }
            list.add(temp);
            //System.out.println(list);
        }
        return list;
    }


    public HJHStatisticalService gethJHStatisticalService() {
        return hJHStatisticalService;
    }

    public void sethJHStatisticalService(HJHStatisticalService hJHStatisticalService) {
        this.hJHStatisticalService = hJHStatisticalService;
    }
}